using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TangLiveRecorder.Models;
using TangLiveRecorder.Services;

namespace TangLiveRecorder
{
    public partial class Data
    {
        private static bool _fileInitialized = false;

        public static List<Models.FileInfos> files = new List<Models.FileInfos>
        {
            new Models.FileInfos
            {
                Guid = "file-001",
                Name = "演示视频",
                Path = @"C:\Users\Public\Videos\demo.mp4",
                Description = "这是一个演示用的视频文件",
                Size = 52428800, // 50MB
                CreatedTime = DateTime.Now.AddDays(-7),
                ModifiedTime = DateTime.Now.AddDays(-3)
            },
            new Models.FileInfos
            {
                Guid = "file-002",
                Name = "背景音乐",
                Path = @"C:\Users\Public\Music\background.mp3",
                Description = "直播背景音乐文件",
                Size = 4194304, // 4MB
                CreatedTime = DateTime.Now.AddDays(-15),
                ModifiedTime = DateTime.Now.AddDays(-10)
            },
            new Models.FileInfos
            {
                Guid = "file-003",
                Name = "配置文档",
                Path = @"C:\Users\Public\Documents\config.txt",
                Description = "系统配置说明文档",
                Size = 2048, // 2KB
                CreatedTime = DateTime.Now.AddDays(-30),
                ModifiedTime = DateTime.Now.AddDays(-1)
            },
            new Models.FileInfos
            {
                Guid = "file-004",
                Name = "封面图片",
                Path = @"C:\Users\Public\Pictures\cover.jpg",
                Description = "直播封面图片",
                Size = 1048576, // 1MB
                CreatedTime = DateTime.Now.AddDays(-5),
                ModifiedTime = DateTime.Now.AddDays(-2)
            },
            new Models.FileInfos
            {
                Guid = "file-005",
                Name = "录制片段",
                Path = @"C:\Users\Public\Videos\record_20241201.avi",
                Description = "昨天的录制片段",
                Size = 134217728, // 128MB
                CreatedTime = DateTime.Now.AddDays(-1),
                ModifiedTime = DateTime.Now.AddHours(-12)
            },
            new Models.FileInfos
            {
                Guid = "file-006",
                Name = "不存在的文件",
                Path = @"C:\NonExistent\missing.file",
                Description = "这个文件不存在，用于测试显示效果",
                Size = 0,
                CreatedTime = DateTime.Now.AddDays(-100),
                ModifiedTime = DateTime.Now.AddDays(-100)
            }
        };

        // 文件相关事件
        public static event EventHandler<FileEventArgs> FileAdded;

        public static event EventHandler<FileEventArgs> FileRemoved;

        public static event EventHandler FilesCleared;

        /// <summary>
        /// 初始化文件数据持久化服务
        /// </summary>
        /// <param name="persistenceService">持久化服务实例</param>
        public static void InitializeFilePersistence(DataPersistenceService persistenceService)
        {
            if (_fileInitialized) return;

            _persistenceService = persistenceService;

            // 加载已保存的数据
            var savedFiles = _persistenceService.LoadFiles();
            if (savedFiles.Count > 0)
            {
                files.Clear();
                files.AddRange(savedFiles);
                AddLog($"从持久化存储加载了 {savedFiles.Count} 个文件记录");
            }

            _fileInitialized = true;
        }

        /// <summary>
        /// 保存文件数据到持久化存储
        /// </summary>
        private static void SaveFilesToPersistence()
        {
            if (_persistenceService != null && _fileInitialized)
            {
                Task.Run(() => _persistenceService.SaveFilesAsync(new List<Models.FileInfos>(files)));
            }
        }

        // 移除文件
        public static void RemoveFile(string guid)
        {
            var file = files.FirstOrDefault(f => f.Guid == guid);
            if (file != null)
            {
                files.Remove(file);
                FileRemoved?.Invoke(null, new FileEventArgs(file));
                AddLog($"移除文件: {file.Name}");
                SaveFilesToPersistence();
            }
        }

        // 清空所有文件
        public static void ClearFiles()
        {
            var count = files.Count;
            files.Clear();
            FilesCleared?.Invoke(null, EventArgs.Empty);
            AddLog($"清空文件列表，共移除 {count} 个文件");
            SaveFilesToPersistence();
        }

        // 获取所有文件
        public static List<Models.FileInfos> GetAllFiles()
        {
            return new List<Models.FileInfos>(files);
        }

        internal static void AddFile(FileInfos fileInfoModel)
        {
            files.Add(fileInfoModel);
            FileAdded?.Invoke(null, new FileEventArgs(fileInfoModel));
            AddLog($"添加文件: {fileInfoModel.Name} ({fileInfoModel.SizeFormatted})");
        }

        // 移除选择相关方法

        // 文件事件参数类
        public class FileEventArgs : EventArgs
        {
            public Models.FileInfos File { get; }

            public FileEventArgs(Models.FileInfos file)
            {
                File = file;
            }
        }
    }
}